package com.ncp.model.biz.order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.jstl.sql.Result;

import com.ncp.model.dao.DB;
import com.ncp.model.dao.DbConnection;
import com.ncp.model.entity.order.Receiver;

public class ManageAddressBiz {
DB common = new DB();
	
	/**
	 * 取得用户的常用收货地址
	 * @param userid
	 */
	public List<Receiver> getReceivers(int userID){
		List receivers = new ArrayList<Receiver>();
		String sql = "select id, name, address, email, phone, mobile, zip, userid from RECEIVER where userID = " + userID;
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		Result rs = common.executeQuery();
		for(Map o : rs.getRows()){
			Receiver receiver = new Receiver();
			
			receiver.setId(Integer.parseInt(o.get("ID").toString()));
			receiver.setName(o.get("NAME").toString());
			receiver.setAddress(o.get("ADDRESS").toString());
			receiver.setEmail(o.get("EMAIL").toString());
			receiver.setPhone(o.get("PHONE").toString());
			receiver.setMobile(o.get("MOBILE").toString());
			receiver.setZip(o.get("ZIP").toString());
			receiver.setUserID(Integer.parseInt(o.get("USERID").toString()));
			
			receivers.add(receiver);
		}
		
		return receivers;
	}
	
	/**
	 * 根据地址ID取得地址
	 * @param receiverID
	 */
	public Receiver getReceiverByID(int receiverID){
		String sql = "select id, name, address, email, phone, mobile, zip, userid from RECEIVER where id = " + receiverID;
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		Result rs = common.executeQuery();
		Receiver receiver = new Receiver();
		for(Map o : rs.getRows()){
						
			receiver.setId(Integer.parseInt(o.get("ID").toString()));
			receiver.setName(o.get("NAME").toString());
			receiver.setAddress(o.get("ADDRESS").toString());
			receiver.setEmail(o.get("EMAIL").toString());
			receiver.setPhone(o.get("PHONE").toString());
			receiver.setMobile(o.get("MOBILE").toString());
			receiver.setZip(o.get("ZIP").toString());
			receiver.setUserID(Integer.parseInt(o.get("USERID").toString()));
			
		}
		
		return receiver;
	}
	
	/**
	 * 增加收货地址
	 *@param Receiver
	 *@return 主键
	 */
	public int addReceiver(Receiver receiver){
		int id = 0;
		String sql = "insert into RECEIVER (name, address, zip, phone, mobile, email, userid) " +
				"values('"+ receiver.getName() +"','"+ receiver.getAddress() +"','"+ receiver.getZip() +"','"+ receiver.getPhone() +"','"+ receiver.getMobile() +"','"+ receiver.getEmail()+"','"+ receiver.getUserID()+"')";
		Connection conn = DbConnection.getConnByDataSource();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql, ps.RETURN_GENERATED_KEYS);
			ps.execute();
			rs = ps.getGeneratedKeys();
			if(rs.next()){
				id = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				ps.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return id;
	}
	
	/**
	 * 删除旧地址
	 * @param receiverID
	 */
	public int deleteReceiver(String receiverID){
		int result = 0;
		String sql = "delete from RECEIVER where id = '"+ receiverID +"'";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		result = common.executeUpdate();
		return result;
	}
}
